package com.cyz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.cyz.exception.SessionIsExistException;
import com.cyz.mapper.ReserveMapper;
import com.cyz.mapper.SessionMapper;
import com.cyz.mapper.TypeMapper;
import com.cyz.pojo.Paging;
import com.cyz.pojo.Reserve;
import com.cyz.pojo.Session;
import com.cyz.pojo.Type;
import com.cyz.service.SessionService;

/**
 * @author 陈雅珠
 * 场次服务层实现类
 */
@Service("sessionServiceImpl")
public class SessionServiceImpl implements SessionService,ApplicationListener<ContextRefreshedEvent> {

	@Resource(name="reserveMapper")
	private ReserveMapper reserveMapper;
	@Resource(name="sessionMapper")
	private SessionMapper sessionMapper;
	@Resource(name="paging")
	private Paging<Session> paging;
	@Resource(name="session")
	private Session session;
	@Resource(name="typeMapper")
	private TypeMapper typeMapper;
	
	/**
	 * 根据不同的条件获取场次列表
	 * @param map 必填：page 当前页，pagSize 页面大小 可选: place 场地 ，startTime 起始时间，endTime 结束时间， type 考试类型， subject 科目， 
	 * @return paging 场次列表
	 */
	@Override
	public Paging<Session> getSession(Map map) {
		
		Integer page = (Integer)map.get("page");
		Integer pageSize = (Integer)map.get("pageSize");
		map.put("start",(page-1)*pageSize);
		map.put("offset",pageSize);
		if(null == (String)map.get("startTime")) {
			map.put("startTime","");
		}
		if(null == (String)map.get("endTime")) {
			map.put("endTime","");
		}
		if(null == (String)map.get("place")) {
			map.put("place","");
		}
		if(null == (String)map.get("typeName")) {
			map.put("typeName","");
		}
		if(null == (String)map.get("subject")) {
			map.put("subject","");
		}
		paging.setCurrentPage(page);
		paging.setPageSize(pageSize);
		paging.setList(sessionMapper.getSession(map));
		paging.setTotalCount(sessionMapper.getSessionTotalCount(map));
		return paging;
	}

	/**
	 * 服务器启动后session进行初始化
	 */
	@Override  
	public void onApplicationEvent(ContextRefreshedEvent arg0) {  
		sessionMapper.sessionInit();
	}

	/**
	 * 添加场次
	 * @param typeName 考试类型名称
	 * @param dates 考试日期
	 * @param times 时间段
	 * @param remain 剩余人数
	 * @param place 场地
	 * @param subject 科目
	 * @throws SessionIsExistException 场次已存在异常
	 * @return void 
	 */
	@Override
	public void addSession(String typeName, String dates, 
			String times, Integer remain, String place, String subject) 
					throws SessionIsExistException {
	
		
		//判断该场次是否已经存在，如存在就抛出异常
		Map map = new HashMap();
		map.put("state",1);
		map.put("start",0);
		map.put("offset",1);
		map.put("subject",subject);
		map.put("typeName",typeName);
		map.put("place",place);
		map.put("startTime",""); 
		map.put("endTime","");
		List<Session> list = sessionMapper.getSession(map);
		if(0 != list.size()) { //场次已存在
			throw new SessionIsExistException();
		} else {
			session.setPlace(place);
			session.setRemain(remain);
			session.setSubject(subject);
			session.setTimes(times);
			session.setType(typeMapper.getTypeByName(typeName));
			session.setDates(dates);
			session.setState(1);
			sessionMapper.addSession(session);
		}
		
	}

	/**
	 *  通过场次编号删除场次 
	 * @param sessionId 场次编号
	 * @return void
	 */
	@Override
	public void deleteSession(Integer sessionId) {
		sessionMapper.deleteSession(sessionId);
		//把该场次的预约全取消,官方取消状态设为2
		reserveMapper.deleteReserveBySessionId(sessionId);
	}
	
}
